import Vue from 'vue'
import VueRouter from 'vue-router'
import index from "../views/index/index"
import {appAll} from "../api/appAll";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'first',
    redirect: "/index"
  },
  {
    path: "/index",
    name: 'index',
    component: index
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/login/login')
  },
  {
    path: '/detail',
    name: 'detail',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/detail/detail')
  },
  {
    path: '/myroom',
    name: 'myroom',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/myroom/myroom'),
    meta:{
      needChoose:true
    }
  },
  {
    path: '/search',
    name: 'search',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/search/search')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,form,next)=>{
  if (to.meta.needChoose==true){
    appAll().then(res =>{
      if(res.code==0){
        next();
      }else{
        next('index')
      }
    })
  }
  else{
    next()
  }
})

export default router
